package edu.gmu.middleware;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * The Sink defines behavior for a source and destination of byte[]s.
 * This could be UDPSockets, TCPSockets, File, or even in-memory.
 * 
 * @author  James H. Pope
 * @version $Revision:$ $Date:$
 */
public interface Sink
{
    
    /**
     * Opens the Sink for reading/writing.
     * @exception IOException if an error occurs.
     */
    public void connect() throws IOException;
    
    /**
     * Closes the Sink for reading/writing.
     * @exception IOException if an error occurs.
     */
    public void disconnect();
    
    /**
     * Determones if Sink is open for reading/writing.
     * @return true if connected, false otherwise
     */
    public boolean isConnected();
    
    /**
     * Writes the contents of buffer to the Sink.  Though not as
     * significant as a read, this call is also blocking.
     * @param out
     * @return number of bytes sent
     */
    public int write( ByteBuffer out ) throws IOException;
    
    /**
     * Reads from the Sink (usually a network Socket) into buffer.
     * This method blocks until data is ready.
     * @param in
     * @return Header (peeked header - position of in is at first byte header)
     */
    public Header read( ByteBuffer in ) throws IOException;
}
